/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.peopleinformation.organization.web;

import java.util.List;

import com.jeeplus.modules.peopleinformation.basicdata.service.PeopleInformationService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.peopleinformation.organization.entity.NameOfOrganization;
import com.jeeplus.modules.peopleinformation.organization.service.NameOfOrganizationService;

import javax.servlet.http.HttpServletRequest;

/**
 * organizationInformationController
 * @author 秦子洋
 * @version 2021-08-04
 */
@RestController
@RequestMapping(value = "/peopleinformation/organization/nameOfOrganization")
public class NameOfOrganizationController extends BaseController {

	@Autowired
	private NameOfOrganizationService nameOfOrganizationService;

	@Autowired
	private PeopleInformationService peopleInformationService;

	@ModelAttribute
	public NameOfOrganization get(@RequestParam(required=false) String id) {
		NameOfOrganization entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = nameOfOrganizationService.get(id);
		}
		if (entity == null){
			entity = new NameOfOrganization();
		}
		return entity;
	}


	/**
	 * organizationInformation树表数据
	 */
//	@RequiresPermissions("peopleinformation:organization:nameOfOrganization:list")
	@GetMapping("list")
	public AjaxJson list(NameOfOrganization nameOfOrganization) {
		return AjaxJson.success().put("list", nameOfOrganizationService.findList(nameOfOrganization));
	}

	/**
	 * 根据Id获取organizationInformation数据
	 */
//	@RequiresPermissions(value={"peopleinformation:organization:nameOfOrganization:view","peopleinformation:organization:nameOfOrganization:add","peopleinformation:organization:nameOfOrganization:edit"},logical=Logical.OR)
	@GetMapping("queryById")
	public AjaxJson queryById(NameOfOrganization nameOfOrganization) {
		return AjaxJson.success().put("nameOfOrganization", nameOfOrganization);
	}

	/**
	 * 保存organizationInformation
	 */
//	@RequiresPermissions(value={"peopleinformation:organization:nameOfOrganization:add","peopleinformation:organization:nameOfOrganization:edit"},logical=Logical.OR)
	@PostMapping("save")
	public AjaxJson save(NameOfOrganization nameOfOrganization, Model model, HttpServletRequest request) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(nameOfOrganization);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		nameOfOrganizationService.save(nameOfOrganization);//保存
		//实现级联功能
		String oneInstitutional = nameOfOrganization.getOrganization();
		String oldOneInstitutional = request.getParameter("oldOneInstitutional");
		peopleInformationService.updateOneInstitutional(oneInstitutional,oldOneInstitutional);
		return AjaxJson.success("保存organizationInformation成功");
	}

	/**
	 * 删除organizationInformation
	 */
//	@RequiresPermissions("peopleinformation:organization:nameOfOrganization:del")
	@DeleteMapping("delete")
	public AjaxJson delete(NameOfOrganization nameOfOrganization) {
		//实现级联功能
		String oldOneInstitutional = nameOfOrganization.getOrganization();
		String oneInstitutional = null;
		peopleInformationService.updateOneInstitutional(oneInstitutional,oldOneInstitutional);
		nameOfOrganizationService.delete(nameOfOrganization);
		return AjaxJson.success("删除organizationInformation成功");
	}

	/**
     * 获取JSON树形数据。
     * @param extId 排除的ID
     * @return
	*/
	@RequiresPermissions("user")
	@GetMapping("treeData")
	public AjaxJson treeData(@RequestParam(required = false) String extId) {
		List<NameOfOrganization> list = nameOfOrganizationService.findList(new NameOfOrganization());
		List rootTree =  nameOfOrganizationService.formatListToTree (new NameOfOrganization ("0"),list, extId );
		return AjaxJson.success().put("treeData", rootTree);
	}

}